﻿using System;
using System.Linq;
using libtcod;

namespace RL
{
    public class GraphicsEngine
    {
        private const int FPS = 30;
        private const string FONT = "resources\\arial12x12.png";
        public const int SCREEN_WIDTH = 120;
        public const int SCREEN_HEIGHT = 60;
        public const int MAIN_WIDTH = SCREEN_WIDTH*3/4;
        public const int MAIN_HEIGHT = SCREEN_HEIGHT;
        public const int PANEL_WIDTH = SCREEN_WIDTH - MAIN_WIDTH;
        public const int PANEL_HEIGHT = SCREEN_HEIGHT;


        /// <summary>
        /// The left hand side main panel of the screen
        /// </summary>
        private readonly TCODConsole _main;
        /// <summary>
        /// The right hand side panel of the screen
        /// </summary>
        private readonly TCODConsole _panel;
        /// <summary>
        /// The background of the screen which is the blue border
        /// </summary>
        private readonly TCODConsole _screenBack;
        private int _panelLine = 0;

        public GraphicsEngine()
        {
            TCODMouse.showCursor(true);
            TCODSystem.setFps(FPS);

            _main = new TCODConsole(MAIN_WIDTH, MAIN_HEIGHT);

            _screenBack = new TCODConsole(MAIN_WIDTH, MAIN_HEIGHT);
            _screenBack.setBackgroundColor(TCODColor.lightBlue);
            foreach (var x in Enumerable.Range(0, SCREEN_WIDTH))
            {
                foreach (var y in Enumerable.Range(0, SCREEN_HEIGHT))
                {
                    _screenBack.printEx(x, y, TCODBackgroundFlag.Screen, TCODAlignment.CenterAlignment, " ");
                }
            }

            _panel = new TCODConsole(PANEL_WIDTH, PANEL_HEIGHT);
            _panel.setForegroundColor(TCODColor.white);
        }

        /// <summary>
        /// Renders the _main and the _panel to the monitor and flushes it.
        /// </summary>
        public void RenderToScreen()
        {
            TCODConsole.blit(_screenBack, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, TCODConsole.root, 0, 0);
            TCODConsole.blit(_panel, 0, 0, PANEL_WIDTH - 1, PANEL_HEIGHT - 2, TCODConsole.root, MAIN_WIDTH, 1);
            _panelLine = 1;
            TCODConsole.blit(_main, 0, 0, MAIN_WIDTH - 2, MAIN_HEIGHT - 2, TCODConsole.root, 1, 1);

            TCODConsole.flush();            
        }

        /// <summary>
        /// Draws the map to the _main part of the screen with center point
        /// </summary>
        public void DrawToMain(TCODConsole mapBuffer, Rect window)
        {
            TCODConsole.blit(
                mapBuffer, 
                Math.Min(Math.Max(window.TopLeft.X, 0), mapBuffer.getWidth() - MAIN_WIDTH), 
                Math.Min(Math.Max(window.TopLeft.Y, 0), mapBuffer.getHeight() - MAIN_HEIGHT), 
                MAIN_WIDTH, 
                MAIN_HEIGHT, 
                _main, 
                0, 
                0);
        }

        public void DrawToPanel(string str)
        {
            _panel.print(1, _panelLine++, str);
        }

        public void ToggleFullScreen()
        {
            TCODConsole.setFullscreen(!TCODConsole.isFullscreen());
        }        
    }
}